package cn.lanqiao.bigDate.web.controller;

import cn.lanqiao.bigDate.entity.sys.SysDept;
import cn.lanqiao.bigDate.reslut.Result;
import cn.lanqiao.bigDate.service.excle.ExcelService;
import cn.lanqiao.bigDate.web.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(SysDeptController.BASE_URL)
public class SysDeptController extends BaseController {
    public static final String BASE_URL = "sysDept";
    /**
     * 页面跳转
     * @return
     */
    @RequestMapping(value = "toIndex")
    public ModelAndView toIndex() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(BASE_URL + "/index"); //指定路径
        return mav;
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping(value = "findSysDeptPage")
    public IPage<SysDept> findByPage(Page<SysDept> page,String searchDeptName,String searchStatus) {
        return sysDeptService.findByPage(page,searchDeptName,searchStatus);
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping(value = "findSysDeptOne")
    public SysDept findSysDeptOne(Long id) {
        return sysDeptService.findSysDeptOne(id);
    }

    /**
     * 修改用户信息
     * @param sysDept
     * @return
     */
    @PostMapping(value = "updateSysDept")
    public Result<String> findSysDeptOne(SysDept sysDept) {
        Result<String> reslut = new Result();
        if (sysDept.getParentId()==null && sysDept.getDeptName().equals("") && sysDept.getOrderNum()==null){
            reslut.setCode(500);
            reslut.setMsg("修改失败");
        }else {
            Boolean isSuccess = sysDeptService.updateSysDept(sysDept);
            if(isSuccess){
                reslut.setCode(200);
                reslut.setMsg("修改成功");
            }else{
                reslut.setCode(500);
                reslut.setMsg("修改失败");
            }
        }

        return reslut;
    }
    /**
     * 保存用户信息
     * @param sysDept
     * @return
     */
    @PostMapping(value = "saveSysDept")
    public Result<String> saveSysDept(SysDept sysDept) {
        Result<String> reslut = new Result();
        if (sysDept.getParentId()==null && sysDept.getDeptName().equals("") && sysDept.getOrderNum()==null){
            reslut.setCode(500);
            reslut.setMsg("添加失败");
        }else {
            Boolean isSuccess = sysDeptService.saveSysDept(sysDept);
            if(isSuccess){
                reslut.setCode(200);
                reslut.setMsg("添加成功");
            }else{
                reslut.setCode(500);
                reslut.setMsg("添加失败");
            }
        }

        return reslut;
    }

    /**
     * 按id删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "deleteSysDept")
    public Result<String> deleteSysDept(String ids) {
        Result<String> reslut = new Result();
        Boolean isSuccess = sysDeptService.deleteSysDept(ids);
        if(isSuccess){
            reslut.setCode(200);
            reslut.setMsg("删除成功");
        }else{
            reslut.setCode(500);
            reslut.setMsg("删除失败");
        }
        return reslut;
    }


    /**
     * 导入数据
     * @param request
     * @return
     */
    @PostMapping(value = "importDeptExcel")
    public Result<String> importOrderData(HttpServletRequest request) {
        Result<String> result = new Result<>();
        if (!(request instanceof MultipartHttpServletRequest)) {
            result.setCode(200);
            return result;
        }
        try {
            List<MultipartFile> files = ((MultipartHttpServletRequest) request)
                    .getFiles("file-2");
            for (MultipartFile file : files) {
                String fileName = file.getOriginalFilename();
                long size = file.getSize();
                if (size == 0) {
                    result.setCode(500);
                    result.setMsg("文件为空");
                    return result;
                }
                String[] name = fileName.split("\\.");
                //默认为高级版excel
                int type = 1;
                if ("xls".equals(name[1])) {
                    type = 2;
                }
                List<SysDept> sheet1 = excelService.readExcelOfList(file.getInputStream(), "sheet1", SysDept.class);
                System.out.println(sheet1);
            }
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

}
